<?php
namespace app\admin\controller;

use app\AdminCollection;
use app\model\Admin;
use app\model\AdminGroup;
use app\model\AdminLoginLog;
use app\model\UpdateBalancemodel;
use app\model\IntegralLogmodel;
use app\model\Members;
use app\model\Modeltable;
use app\model\Modelfield;
use app\model\Menu;
use My\Tool;
use think\Db;

class Index extends AdminCollection
{

    private $rules = null;

    public function __construct()
    {
        parent::__construct();
        if ($this->getGroup() != config('default_group_id')) {
            $adminGroupFind = AdminGroup::findService(['admin_group_id' => $this->getGroup()], ['rules']);
            $this->rules = $adminGroupFind['rules'];
        }
    }

    public function index()
    {
        return $this->fetch();
    }

    /**
     * 主体头部菜单
     * @return mixed
     */
    public function header()
    {
        $where = array();
        $where['pid'] = 0;
        $where['status'] = 1;
        $where['is_display'] = 1;
        if ($this->getGroup() != config('default_group_id')) {
            $where['menu_id'] = ['in', $this->rules];
        }
        $headerMenuList = Menu::listService($where);
        $_list = array();
        foreach ($headerMenuList as $key => $val) {
            $_t = $val;
            $_t['sidebarurl'] = url($val['name'], array('menuid' => $val['menu_id']));
            $_list[] = $_t;
        }
        $this->assign('headerMenuList', $_list);
        return $this->fetch();
    }

    /**
     * 主体左边菜单
     * @return mixed
     */
    public function sidebar()
    {
        $menuid = input('menuid') ? input('menuid') : 1;
        $where = array();
        $where['pid'] = $menuid;
        $where['status'] = 1;
        $where['is_display'] = 1;
        if ($this->getGroup() != config('default_group_id')) {
            $where['menu_id'] = ['in', $this->rules];
        }
        $sidebarMenuList = Menu::listService($where);
        $_list = array();
        foreach ($sidebarMenuList as $key => $val) {
            $_t = $val;
            $_child = array();
            $_child_where = array();
            $_child_where['pid'] = $val['menu_id'];
            $_child_where['status'] = 1;
            $_child_where['is_display'] = 1;
            if ($this->getGroup() != config('default_group_id')) {
                $_child_where['menu_id'] = ['in', $this->rules];
            }
            $childData = Menu::listService($_child_where);
            foreach ($childData as $k => $v) {
                $_c = $v;
                $_c['mainurl'] = url($v['name'], array('menuid' => $v['menu_id']));
                $_child[] = $_c;
            }
            $_t['child'] = $_child;
            $_list[] = $_t;
        }
        $this->assign('sidebarMenuList', $_list);
        return $this->fetch();
    }

    public function upload()
    {
        return $this->fetch();
    }

    public function test()
    {
        return $this->fetch();
    }

    /**
     * 主体右边内容
     * @return mixed
     */
    public function main()
    {
        //管理员登录记录
        $adminloginlog = AdminLoginLog::listLimitService();
        foreach ($adminloginlog as $key => $val) {
            $_all = $val;
            $_all['login_time'] = $val['login_time'] != 0 ? date('Y-m-d H:i:s', $val['login_time']) : '0000-00-00 00:00:00';
            $adminloginlog[$key] = $_all;
        }
        $this->assign('adminloginlog', $adminloginlog);

        //会员登录记录
        $memberloginlog = Members::listLimitService();
        foreach ($memberloginlog as $keym => $val) {
            $_mll = $val;
            $_mll['login_time'] = $val['login_time'] != 0 ? date('Y-m-d H:i:s', $val['login_time']) : '0000-00-00 00:00:00';
            $memberloginlog[$keym] = $_mll;
        }
        $this->assign('memberloginlog', $memberloginlog);

        //余额记录
        $updatebalancelist = UpdateBalancemodel::listLimitService();
        foreach ($updatebalancelist as $keym => $val) {
            $_ub = $val;
            $adminfind = Admin::findService(['admin_id' => $val['admin_id']], ['username']);
            $_ub['adminusername'] = $adminfind['username'];
            $memberfind = Members::findService(['member_id' => $val['member_id']], ['username']);
            $_ub['memberusername'] = $memberfind['username'];
            if ($val['plusless'] == 1) {
                $_ub['balance'] = '<span style="color: green">+' . Tool::formatbalance($val['balance']) . '</span>';
            } else {
                $_ub['balance'] = '<span style="color: red">-' . Tool::formatbalance($val['balance']) . '</span>';
            }
            $_ub['inputtime'] = $val['inputtime'] != 0 ? date('Y-m-d H:i:s', $val['inputtime']) : '0000-00-00 00:00:00';
            $updatebalancelist[$keym] = $_ub;
        }
        $this->assign('updatebalancelist', $updatebalancelist);

        //积分记录
        $integralloglist = IntegralLogmodel::listLimitService();
        foreach ($integralloglist as $keym => $val) {
            $_ill = $val;
            $adminfindintergral = Admin::findService(['admin_id' => $val['admin_id']], ['username']);
            $_ill['adminusername'] = $adminfindintergral['username'];
            $memberfindintergral = Members::findService(['member_id' => $val['member_id']], ['username']);
            $_ill['memberusername'] = $memberfindintergral['username'];
            if ($val['plusless'] == 1) {
                $_ill['integral'] = '<span style="color: green">+' . $val['integral'] . '</span>';
            } else {
                $_ill['integral'] = '<span style="color: red">-' . $val['integral'] . '</span>';
            }
            $_ill['inputtime'] = $val['inputtime'] != 0 ? date('Y-m-d H:i:s', $val['inputtime']) : '0000-00-00 00:00:00';
            $integralloglist[$keym] = $_ill;
        }
        $this->assign('integralloglist', $integralloglist);
        $this->assign('sys_info', $this->get_sys_info());
        return $this->fetch();
    }

    public function get_sys_info()
    {
        $sys_info['os'] = PHP_OS;
        $sys_info['zlib'] = function_exists('gzclose') ? 'YES' : 'NO';//zlib
        $sys_info['safe_mode'] = (boolean)ini_get('safe_mode') ? 'YES' : 'NO';//safe_mode = Off
        $sys_info['timezone'] = function_exists("date_default_timezone_get") ? date_default_timezone_get() : "no_timezone";
        $sys_info['curl'] = function_exists('curl_init') ? 'YES' : 'NO';
        $sys_info['web_server'] = $_SERVER['SERVER_SOFTWARE'];
        $sys_info['phpv'] = phpversion();
        $sys_info['ip'] = GetHostByName($_SERVER['SERVER_NAME']);
        $sys_info['fileupload'] = @ini_get('file_uploads') ? ini_get('upload_max_filesize') : 'unknown';
        $sys_info['max_ex_time'] = @ini_get("max_execution_time") . 's'; //脚本最大执行时间
        $sys_info['set_time_limit'] = function_exists("set_time_limit") ? true : false;
        $sys_info['domain'] = $_SERVER['HTTP_HOST'];
        $sys_info['memory_limit'] = ini_get('memory_limit');
        $sys_info['version'] = file_get_contents(APP_PATH . 'admin/conf/version.php');
        $mysqlinfo = Db::query("SELECT VERSION() as version");
        $sys_info['mysql_version'] = $mysqlinfo[0]['version'];
        if (function_exists("gd_info")) {
            $gd = gd_info();
            $sys_info['gdinfo'] = $gd['GD Version'];
        } else {
            $sys_info['gdinfo'] = "未知";
        }
        return $sys_info;
    }

}
